//
//  PTSimplePageViewController.m
//  Pods
//
//  Created by aron on 2017/11/23.
//
//

#import "PTSimplePageViewController.h"
#import "PTSimplePageBodyView.h"
#import "PTSimplePageHeaderView.h"
#import "UIView+PTPageKit.h"

#define kTopBarHeight           (42)
#define kNavHeight              (44)
#define kSegmentHeaderHeight    (27)
#define kStatBarHeight          (20)
#define kTabbarHeight           (49)


#define kTopPadding     (60)
#define kBottomPadding  (60)
#define kLeftPadding    (30)
#define kRightPadding   (30)


@interface PTSimplePageViewController ()
@property (nonatomic, strong) PTSimplePageBodyView* simplePageBodyView;
// ugly..两个Header,一个位于前面，前面的显示为高亮选中状态;一个位于后面，后面的显示为灰色未选中状态
@property (nonatomic, strong) PTSimplePageHeaderView* simplePageHeaderView;
@property (nonatomic, strong) PTSimplePageHeaderView* simplePageBackHeaderView;
@property (nonatomic, strong) UIImageView* bgImageView;
@end

@implementation PTSimplePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view insertSubview:self.simplePageBackHeaderView belowSubview:self.bodyView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadData {
    [super reloadData];
    
    [self.simplePageBackHeaderView reloadData];
    
    [self reloadSelectedButtonAtIndex:self.simplePageHeaderView.currentIndex];
}


#pragma mark - ......::::::: PTPageHeaderViewDelegate, PTPageHeaderViewDataSource :::::::......

- (UIView *)pageView:(PTBasePageHeaderView *)headerView headerItemViewAtIndex:(NSInteger)index {
    return nil;
}


#pragma mark - ......::::::: Lazy load :::::::......

- (PTSimplePageHeaderView *)simplePageHeaderView {
    if (!_simplePageHeaderView) {
        _simplePageHeaderView = [[PTSimplePageHeaderView alloc] init];
        _simplePageHeaderView.delegate = self;
        _simplePageHeaderView.dataSource = self;
        _simplePageHeaderView.frame = CGRectMake(kLeftPadding, kTopPadding + 14, self.view.width - kLeftPadding - kRightPadding, kNavHeight);
    }
    return _simplePageHeaderView;
}

- (PTSimplePageHeaderView *)simplePageBackHeaderView {
    if (!_simplePageBackHeaderView) {
        _simplePageBackHeaderView = [[PTSimplePageHeaderView alloc] init];
        _simplePageBackHeaderView.delegate = self;
        _simplePageBackHeaderView.dataSource = self;
        _simplePageBackHeaderView.frame = CGRectMake(kLeftPadding, kTopPadding + 14, self.view.width - kLeftPadding - kRightPadding, kNavHeight);
    }
    return _simplePageBackHeaderView;

}

- (PTSimplePageBodyView *)simplePageBodyView {
    if (!_simplePageBodyView) {
        _simplePageBodyView = [[PTSimplePageBodyView alloc] init];
        _simplePageBodyView.dataSource = self;
        _simplePageBodyView.delegate = self;
        _simplePageBodyView.frame = CGRectMake(kLeftPadding, kNavHeight + kTopPadding, self.view.width - kLeftPadding - kRightPadding, self.view.height - kNavHeight - kTopPadding - kBottomPadding);
    }
    return _simplePageBodyView;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageNamed:@"pop_page_bg_panel"];
        _bgImageView.frame = CGRectMake(kLeftPadding - 10, kNavHeight + kTopPadding, self.view.width - kLeftPadding - kRightPadding + 20, self.view.height - kNavHeight - kTopPadding - kBottomPadding + 10);
    }
    return _bgImageView;
}


#pragma mark - ......::::::: Override :::::::......

- (PTBasePageHeaderView *)headerView {
    return self.simplePageHeaderView;
}

- (PTBasePageBodyView *)bodyView {
    return self.simplePageBodyView;
}


#pragma mark - ......::::::: Private Override :::::::......

- (void)headerView:(PTBasePageHeaderView *)headerView didSelectItemAtIndex:(NSInteger)index {
    [self.bodyView moveToIndex:index];
    
    [self reloadSelectedButtonAtIndex:headerView.currentIndex];
}

- (void)reloadSelectedButtonAtIndex:(NSInteger)index {
    [self.simplePageHeaderView selectButtonAtIndex:index isBackMode:NO];
    [self.simplePageBackHeaderView selectButtonAtIndex:index isBackMode:YES];
}

@end
